package com.bolingcavalry.crm.aftersales.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.base.PageResult;
import com.base.ResponseResult;
import com.bolingcavalry.crm.aftersales.service.AfterSalesService;
import com.dto.aftersales.AfterSalesDTO;
import com.vo.AfterSales.AfterSalesStatisVO;
import com.vo.AfterSales.AfterSalesVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @Description: 售后总括控制器
 */
@RestController
@RequestMapping("/aftersales")
@Slf4j
public class AfterSalesController {

    @Autowired
    private AfterSalesService afterSalesService;

    /**
     * 根据id查询售后问题
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public ResponseResult<AfterSalesVO> getById(@PathVariable Long id) {
        return ResponseResult.success(afterSalesService.getById(id));
    }

    /**
     * 根据售后编号查询售后问题
     * @param orderNumber
     * @return
     */
    @GetMapping("/getByon/{ordernumber}")
    public ResponseResult<AfterSalesVO> getByOrderNumber(@PathVariable("ordernumber") String orderNumber) {
        Long idByServiceNumber = afterSalesService.getIdByServiceNumber(orderNumber);
        return ResponseResult.success(afterSalesService.getById(idByServiceNumber));
    }

    /**
     * 创建售后问题
     * @param afterSales
     * @return
     */
    @PostMapping("/add")
    public ResponseResult create(@RequestBody AfterSalesDTO afterSales) {
        return afterSalesService.save(afterSales);
    }

    /**
     * 更新售后问题
     * @param afterSales
     * @return
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody AfterSalesDTO afterSales) {
        if(afterSalesService.update(afterSales)){
            return ResponseResult.success("售后问题更新成功");
        }
        return ResponseResult.error("售后问题更新失败");
    }

    /**
     * 根据id删除售后问题
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable Long id) {
       return afterSalesService.deleteById(id);
    }

    /**
     * 分页查询售后问题
     */
    @GetMapping("/list")
    public ResponseResult<AfterSalesVO> list(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false,defaultValue = "10") Integer pageSize,
            @RequestParam(value = "serviceNumber", required = false) String serviceNumber,//任务单号
            @RequestParam(value = "customerName", required = false) String customerName,//客户名称
            @RequestParam(value = "receiverName", required = false) String receiverName,//接收人员姓名
            @RequestParam(value = "startTime", required = false) String startTime,//售后开始时间
            @RequestParam(value = "endTime", required = false) String endTime,//售后结束时间
            @RequestParam(value = "status", required = false) String status,//状态
            @RequestParam(value = "keyword", required = false) String keyword,//统一字段
            @RequestParam(value = "projectType", required = false) String projectType//项目类型
    ){
        IPage<AfterSalesVO> page = afterSalesService.getByConditions(pageNum, pageSize, serviceNumber, customerName,receiverName,startTime,endTime, status, keyword, projectType);
        PageResult<AfterSalesVO> result = new PageResult<>();
        result.setTotal(page.getTotal());
        result.setPages((int) page.getPages());
        result.setCurrent((int) page.getCurrent());
        result.setSize((int) page.getSize());
        result.setRecords(page.getRecords());
        return ResponseResult.success("查询成功",result);
    }

    /**
     * 查询总数count（本月售后问题、全年售后问题、当前进行中、本月已完结四项的数据？）
     */
    @GetMapping("/count")
    public ResponseResult<AfterSalesStatisVO> count(){
        return ResponseResult.success("查询成功",afterSalesService.getcount());
    }

    /**
     * 根据售后编号查询id
     */
    @GetMapping("/getIdByServiceNumber/{orderNumber}")
    public ResponseResult<Long> getIdByServiceNumber(@PathVariable String orderNumber) {
        return ResponseResult.success("查询成功",afterSalesService.getIdByServiceNumber(orderNumber));
    }



}